package com.binarywang.spring.starter.wxjava.tp.storage;

import com.binarywang.spring.starter.wxjava.tp.properties.WxCpTpProperties;
import me.chanjar.weixin.cp.config.impl.WxCpTpDefaultConfigImpl;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.util.Objects;

/**
 * WxCpConfigStorage 抽象配置类
 *
 * @author yl & Wang_Wong
 * created on  2021/12/6
 */
public abstract class AbstractWxCpTpConfigStorageConfiguration {

  protected WxCpTpDefaultConfigImpl config(WxCpTpDefaultConfigImpl config, WxCpTpProperties properties) {
    String corpId = properties.getCorpId();
    String token = properties.getToken();
    String aesKey = properties.getAesKey();
    String suiteId = properties.getSuiteId();
    String baseApiUrl = properties.getBaseApiUrl();
    String providerSecret = properties.getProviderSecret();

    File tmpDirFile = properties.getTmpDirFile();

    config.setCorpId(corpId);

    if (StringUtils.isNotBlank(token)) {
      config.setToken(token);
    }
    if (StringUtils.isNotBlank(suiteId)) {
      config.setSuiteId(properties.getSuiteId());
    }
    if (StringUtils.isNotBlank(aesKey)) {
      config.setAesKey(aesKey);
    }
    if (StringUtils.isNotBlank(baseApiUrl)) {
      config.setBaseApiUrl(baseApiUrl);
    }
    if (StringUtils.isNotBlank(providerSecret)) {
      config.setProviderSecret(providerSecret);
    }

    if (Objects.isNull(tmpDirFile)) {
      config.setTmpDirFile(tmpDirFile);
    }


    WxCpTpProperties.ConfigStorage storage = properties.getConfigStorage();
    String httpProxyHost = storage.getHttpProxyHost();
    Integer httpProxyPort = storage.getHttpProxyPort();
    String httpProxyUsername = storage.getHttpProxyUsername();
    String httpProxyPassword = storage.getHttpProxyPassword();
    if (StringUtils.isNotBlank(httpProxyHost)) {
      config.setHttpProxyHost(httpProxyHost);
      if (httpProxyPort != null) {
        config.setHttpProxyPort(httpProxyPort);
      }
      if (StringUtils.isNotBlank(httpProxyUsername)) {
        config.setHttpProxyUsername(httpProxyUsername);
      }
      if (StringUtils.isNotBlank(httpProxyPassword)) {
        config.setHttpProxyPassword(httpProxyPassword);
      }
    }
    return config;
  }

}
